// pages/category/category.js

import {
  getCategory,
  getSubcategory,
  getCategoryDetail
} from "../../service/category"

import * as Constant from "../../common/constant"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTitles: [{
        type: Constant.POP,
        title: '综合'
      },
      {
        type: Constant.NEW,
        title: '新品'
      },
      {
        type: Constant.SELL,
        title: '销量'
      }
    ],
    categoryList: [],
    currentKey: null,
    currentMiniWallkey: null,
    subcategoryList: [],
    currentType: null,
    goodsList: [],
    detailUrl: '/pages/detail/detail',
    showBackToTop: false,
    scrollTop: 0,
    isFixed: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getCategory();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.createSelectorQuery()
      .select("#scrollview")
      .node()
      .exec(res => {
        this.setData({
          scrollViewContext: res[0].node
        });
      })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 监听用户滑动页面事件
   */
  scroll(options) {
    let backToTopFlag = options.detail.scrollTop > Constant.showBackToTopDistance;
    if (backToTopFlag !== this.data.showBackToTop) {
      this.setData({
        showBackToTop: backToTopFlag
      })
    }
  },
  scrolltolower() {
    this._getCategoryDetail(this.data.currentType);
  },

  _getCategory() {
    getCategory().then(res => {
      const categoryList = res.data.list;
      if (categoryList.length > 0) {
        const currentKey = categoryList[0].maitKey;
        const currentMiniWallkey = categoryList[0].miniWallkey;
        this._getSubcategory(currentKey);
        this.setData({
          categoryList,
          currentKey,
          currentMiniWallkey
        })
      }
    })
  },
  _getSubcategory(maitkey) {
    getSubcategory(maitkey).then(res => {
      this._getCategoryDetail(Constant.POP);
      const subcategoryList = res.data.list;
      this.setData({
        subcategoryList
      })
    })
  },
  _getCategoryDetail(currentType) {
    let goodsList = [];
    if (this.data.currentType === currentType) {
      goodsList = this.data.goodsList;
    }
    getCategoryDetail(this.data.currentMiniWallkey, currentType).then(res => {
      goodsList.push(...res.data);
      this.setData({
        currentType,
        goodsList
      })
    });
  },
  categoryTitleClick(event) {
    this.handleBackToTop();
    this._getSubcategory(event.detail.maitkey)
  },
  tabControlItemClick(event) {
    this._getCategoryDetail(event.detail.type);
    this.data.scrollViewContext.scrollIntoView('#goodsList');
  },
  handleBackToTop() {
    this.setData({
      isFixed: false
    })
    this.data.scrollViewContext.scrollIntoView('#goodsView');
  }
})